package com.example.hellofxend.program;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.example.hellofxend.ConnectionBasic;
import com.example.hellofxend.PoertyData.JsonRootBean;
import com.example.hellofxend.QqData.Message;
import com.example.hellofxend.data.IPData;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.sql.*;

public class HelloController extends ConnectionBasic {
    @FXML
    private Label myText;
    @FXML
    private TextField ipTxt;
    @FXML
    private Label poertyText;
    @FXML
    private TextField poertyTxt;
    @FXML
    private Label qqText;
    @FXML
    private  TextField qqTxt;

    @FXML
    protected void onHelloButtonClick() {
        //起到一个Loading的效果
        //myText.setText("Loading……");
        String ipVal = ipTxt.getText();
        //异步机制
        Platform.runLater(() -> {
            String idurl = "https://widget.cifuwu.com/idcard/" + ipVal;
            String result = HttpUtil.get(idurl);
            IPData ipData = JSONUtil.toBean(result, IPData.class);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /**检索数据在数据库内存在，则优先从数据库中读出数据,否则从接口读出数据后再写入到数据库中**/
            //判断数据是否在数据库中存在
            try {
                Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
                String sql_select = "select * from identityinformation where idcard=?";
                PreparedStatement psql_select = conn.prepareStatement(sql_select);
                psql_select.setString(1,ipVal);
                psql_select.executeQuery();
                ResultSet rs = psql_select.executeQuery();
                //存在则从数据库中输出数据
                if(rs.next()){
                    System.out.println("<<"+ipVal+">>"+"在数据库中！所以从数据库中获取"+ipVal+"的相关信息！");
                    String idcard = rs.getString(1);
                    int age = rs.getInt(2);
                    String gender = rs.getString(3);
                    Date solarbirthday = rs.getDate(4);
                    String lunarbirthday = rs.getString(5);
                    String attribute = rs.getString(6);
                    String constellation = rs.getString(7);
                    //System.out.println(idcard +"\n"+age +"\n"+gender + "\n"+solarbirthday+"\n"+lunarbirthday+"\n"+attribute+"\n"+constellation);
                    String outMsg = "数据库中！"+"\n";
                    outMsg += "身份证号码：" + idcard + "\n";
                    outMsg += "年龄:" + age + "\n";
                    outMsg += "性别:" + gender + "\n";
                    outMsg += "阳历生日:" + solarbirthday + "\n";
                    outMsg += "阴历生日:" + lunarbirthday + "\n";
                    outMsg += "属性:" + attribute + "\n";
                    outMsg += "星座:" + constellation + "\n";
                    myText.setText(outMsg);
                    psql_select.close();
                    conn.close();
                }else{
                    System.out.println("<<"+ipVal+">>"+"不在数据库中！所以从接口获取"+ipVal+"的相关信息！并保存进数据库中");
                    //不存在则从接口读出数据，并将数据添加进数据库中
                    String outMsg = "从接口读取！"+"\n";
                    outMsg += "身份证号码：" + ipVal + "\n";
                    outMsg += "年龄:" + ipData.getData().getBorn().getAge() + "\n";
                    outMsg += "性别:" + ipData.getData().getGender().getCn() + "\n";
                    outMsg += "阳历生日:" + ipData.getData().getBorn().getSolar().getBirthday() + "\n";
                    outMsg += "阴历生日:" + ipData.getData().getBorn().getLunar().getChinese_birthday() + "\n";
                    outMsg += "属性:" + ipData.getData().getBorn().getLunar().getChinese_zodiac() + "\n";
                    outMsg += "星座:" + ipData.getData().getBorn().getSolar().getZodiac() + "\n";
                    myText.setText(outMsg);
                    //将数据添加进数据库中
                    try {
                            //Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                            //Statement statement = conn.createStatement();
                            String sql_insert = "insert into identityinformation values(?,?,?,?,?,?,?)";
                            PreparedStatement psql_insert = conn.prepareStatement(sql_insert);
                            psql_insert.setString(1, ipVal);
                            psql_insert.setInt(2, ipData.getData().getBorn().getAge());
                            psql_insert.setString(3,ipData.getData().getGender().getCn() );
                            psql_insert.setDate(4,  ipData.getData().getBorn().getSolar().getBirthday());
                            psql_insert.setString(5, ipData.getData().getBorn().getLunar().getChinese_birthday());
                            psql_insert.setString(6, ipData.getData().getBorn().getLunar().getChinese_zodiac() );
                            psql_insert.setString(7, ipData.getData().getBorn().getSolar().getZodiac());
                            psql_insert.executeUpdate();
                            psql_insert.close();
                            conn.close();
                            } catch (SQLException e) {
                            e.printStackTrace();
                           }
                }

            }catch(SQLException e){
                e.printStackTrace();
            }

         /**根据idcard删除数据**/
//        try {
//           Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//           String sql_delete = "delete from identityinformation where idcard=?";
//           PreparedStatement psql_delete = conn.prepareStatement(sql_delete);
//           psql_delete.setString(1,ipVal) ;
//           psql_delete.executeUpdate();
//           conn.close();
//           System.out.println("数据删除成功");
//            }catch (SQLException e){
//                e.printStackTrace();
//            }
         /**查找数据**/
//         try{
//             Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
//             String sql_select = "select * from identityinformation where idcard`='女'";
//             PreparedStatement psql_select = conn.prepareStatement(sql_select);
//             ResultSet rs = psql_select.executeQuery();
//             while(rs.next()){
//                 String idcard = rs.getString(1);
//                 int age = rs.getInt(2);
//                 String gender = rs.getString(3);
//                 Date solarbirthday = rs.getDate(4);
//                 String lunarbirthday = rs.getString(5);
//                 String attribute = rs.getString(6);
//                 String constellation = rs.getString(7);
//                 System.out.println(idcard +""+age +""+gender + ""+solarbirthday+""+lunarbirthday+""+attribute+""+constellation);
//             }
//             psql_select.close();
//             conn.close();
//         }catch(SQLException e){
//             e.printStackTrace();
//         }

     });
}

    @FXML
    protected void onHelloPoertyClick() {
        //起到一个Loading的效果
        //poertyText.setText("Loading……");
        String poertyVal = poertyTxt.getText();
        //异步机制
        Platform.runLater(() -> {
            String url = "https://qqlykm.cn/api/yan/gc.php";
            String result = HttpUtil.get(url);
            //System.out.println(result);
            JsonRootBean poerty = JSONUtil.toBean(result, JsonRootBean.class);
            //System.out.println(poerty.getData());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断数据是否在数据库中存在
            try {
                Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
                String poerty_select = "select * from poertyinformation where title=?";
                PreparedStatement ppoerty_select = conn.prepareStatement(poerty_select);
                ppoerty_select.setString(1,poertyVal);
                ppoerty_select.executeQuery();
                ResultSet rs = ppoerty_select.executeQuery();
                //存在则从数据库中输出数据
                if(rs.next()){
                    System.out.println("<<"+poertyVal+">>"+"在数据库中！所以从数据库中获取"+poertyVal+"的相关信息！");
                    String title = rs.getString(1);
                    String dynast = rs.getString(2);
                    String author = rs.getString(3);
                    String content = rs.getString(4);
                    //System.out.println(title +"\n"+dynast +"\n"+author + "\n"+content);
                    //String outMsg = "结果:" + poerty.getMsg()+"\n";
                    //outMsg += "id:" + poerty.getData().getId()+"\n";
                    String outMsg = "数据库中！"+"\n";
                    outMsg += "标题:" + title+"\n";
                    outMsg += "朝代:" + dynast+"\n";
                    outMsg += "作者:" + author+"\n";
                    outMsg += "内容:" + content+"\n";
                    poertyText.setText(outMsg);
                    ppoerty_select.close();
                    conn.close();
                     }else{
                            System.out.println("从接口随机获取古诗！并保存进数据库中");
                            //不存在则从接口读出数据，并将数据添加进数据库中
                            String outMsg = "从接口读取！"+"\n";
                            //outMsg += "结果:" + poerty.getMsg()+"\n";
                            //outMsg += "id:" + poerty.getData().getId()+"\n";
                            outMsg += "标题:" + poerty.getData().getSubject()+"\n";
                            outMsg += "朝代:" + poerty.getData().getDynasty()+"\n";
                            outMsg += "作者:" + poerty.getData().getAuthor()+"\n";
                            outMsg += "内容:" + poerty.getData().getContent()+"\n";
                            poertyText.setText(outMsg);
                            //将数据添加进数据库中
                            try {
                                //Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                                //Statement statement = conn.createStatement();
                                String poerty_insert = "insert into poertyinformation values(?,?,?,?)";
                                PreparedStatement ppoerty_insert = conn.prepareStatement(poerty_insert);
                                ppoerty_insert.setString(1, poerty.getData().getSubject());
                                ppoerty_insert.setString(2, poerty.getData().getDynasty());
                                ppoerty_insert.setString(3,poerty.getData().getAuthor() );
                                ppoerty_insert.setString(4,  poerty.getData().getContent());
                                ppoerty_insert.executeUpdate();
                                ppoerty_insert.close();
                                conn.close();
                               } catch (SQLException e) {
                                e.printStackTrace();
                               }
                            }

                        }catch(SQLException e){
                            e.printStackTrace();
                        }
        });

    }

    @FXML
    protected void onHelloQqClick () {
        //起到一个Loading的效果
        //qqText.setText("Loading……");
        String qqVal = qqTxt.getText();
        //异步机制
        Platform.runLater(() -> {
            String url = "https://qqlykm.cn/api/qq/api.php?qq="+qqVal;
            String result = HttpUtil.get(url);
           // System.out.println(result);
            Message qqmessage = JSONUtil.toBean(result, Message.class);

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断数据是否在数据库中存在
            try {
                Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
                String qq_select = "select * from qqinformation where qq=?";
                PreparedStatement pqq_select = conn.prepareStatement(qq_select);
                pqq_select.setString(1,qqVal);
                pqq_select.executeQuery();
                ResultSet rs = pqq_select.executeQuery();
                //存在则从数据库中输出数据
                if(rs.next()){
                    System.out.println("<<"+qqVal+">>"+"在数据库中！所以从数据库中获取"+qqVal+"的相关信息！");
                    String qq = rs.getString(1);
                    String qqname = rs.getString(2);
                    String pictureaddress = rs.getString(3);
                    //System.out.println(qq +"\n"+qqname +"\n"+pictureaddress);
                    String outMsg = "数据库中！"+"\n";
                    outMsg += "qq号:" + qq+"\n";
                    outMsg += "QQ昵称:" + qqname+"\n";
                    outMsg += "头像地址:" + pictureaddress+"\n";
                    qqText.setText(outMsg);
                    pqq_select.close();
                    conn.close();
                }else{
                    //不存在则从接口读出数据，并将数据添加进数据库中
                    System.out.println(qqVal+"不在数据库中！所以从接口获取"+qqVal+"的相关信息，并保存进数据库中");
                    String outMsg = "从接口读取！"+"\n";
                    outMsg += "qq号:" + qqVal+"\n";
                    outMsg += "QQ昵称:" + qqmessage.getName()+"\n";
                    outMsg += "头像地址:" + qqmessage.getImgurl()+"\n";
                    qqText.setText(outMsg);
                    //将数据添加进数据库中
                    try {
                        //Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                        //Statement statement = conn.createStatement();
                        String qq_insert = "insert into qqinformation values(?,?,?)";
                        PreparedStatement pqq_insert = conn.prepareStatement(qq_insert);
                        pqq_insert.setString(1, qqVal);
                        pqq_insert.setString(2, qqmessage.getName());
                        pqq_insert.setString(3,qqmessage.getImgurl());
                        pqq_insert.executeUpdate();
                        pqq_insert.close();
                        conn.close();
                        } catch (SQLException e) {
                             e.printStackTrace();
                        }
                      }
                }catch(SQLException e){
                    e.printStackTrace();
                }

        });

    }

}